package org.dizena.modules.master.dao;

import org.dizena.base.avo.req.JqGridReq;
import org.dizena.base.dao.BaseDao;
import org.dizena.base.dao.DbDao;
import org.dizena.modules.master.bean.User;
import org.dizena.utils.ObjectUtil;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Repository
public class UserDao extends BaseDao implements DbDao<User>
{

    @Resource
    private MongoTemplate dao;


    @Override
    public User add(User m)
    {
        return dao.save(m);
    }

    @Override
    public void deleteById(String id)
    {
        dao.remove(idQuery(id), User.class);
    }

    @Override
    public void deleteBy(String[] keys, Object[] vals)
    {
        Query query = keyIsVal(keys, vals);
        dao.remove(query, User.class);
    }

    @Override
    public void update(User m)
    {
        try
        {
            Update update = new Update();

            Map<String, Object> map = ObjectUtil.formObjectToMap(m);
            for (Map.Entry<String, Object> e : map.entrySet())
            {
                update.set(e.getKey(), e.getValue());
            }
            dao.updateFirst(idQuery(m.getId()), update, User.class);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public User get(String id)
    {
        return dao.findOne(idQuery(id), User.class);
    }

    @Override
    public List<User> findMany(String[] keys, Object[] vals)
    {
        Query query = keyIsVal(keys, vals);
        return dao.find(query, User.class);
    }

    @Override
    public Long count(String[] keys, Object[] vals)
    {
        Query query = keyIsVal(keys, vals);
        return dao.count(query, User.class);
    }

    @Override
    public List<User> find(Long page, Integer size, String[] keys, Object[] vals)
    {
        Query query = keyIsVal(keys, vals);
        if (page != null && size != null)
        {
            query.skip((page - 1) * size);
            query.limit(size);
        }
        return dao.find(query, User.class);
    }

    @Override
    public List<User> queryAll(JqGridReq req, Map<String, Object> map)
    {

        return dao.find(allQuery(req, map), User.class);
    }

    @Override
    public Long count(String searchField, Map<String, Object> map)
    {
        Query query = map2QueryLike(map, searchField);
        return dao.count(query, User.class);
    }

    public User queryAccount(String account)
    {
        Query query = keyIsVal("account", account);
        return dao.findOne(query, User.class);
    }

    //关于聚合计算的示例
    public void aggregate()
    {
        //构造$redact的条件
        Aggregation aggregation = Aggregation.newAggregation(
                //先筛选
                Aggregation.match(Criteria.where("math").gt(30).and("language").lt(100)),
                //在分组
                Aggregation.group("classes").sum("$math").as("mathCount").last("_id").as("id").last("name").as("name"),
                //排除字段
                Aggregation.project().andExclude("name", "gender"));
        //查询
        AggregationResults<User> results = dao.aggregate(aggregation, User.class, User.class);
        List<User> data = results.getMappedResults();

    }

}
